package top.infopub.mgr.ic.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.DesignChangeAttDao;
import top.infopub.mgr.ic.dao.DesignChangeDao;
import top.infopub.mgr.ic.dao.DesignChangeDetailDao;
import top.infopub.mgr.ic.domain.DesignChangeDomain;
import top.infopub.model.ic.DesignChange;
import top.infopub.model.ic.DesignChangeAtt;
import top.infopub.model.ic.DesignChangeDetail;
import top.infopub.mgr.ic.service.DesignChangeService;
import top.infopub.mgr.ic.vo.DesignChangeVo;

@Service
public class DesignChangeServiceImpl extends GenericServiceImpl<DesignChange, String> implements
		DesignChangeService {

	@Autowired
	private DesignChangeDao designChangeDao;
	
	@Autowired
	private DesignChangeDetailDao designChangeDetailDao;
	
	@Autowired
	private DesignChangeAttDao designChangeAttDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<DesignChange, String> getDao() {
		return designChangeDao;
	}

    @Override
    public Page<DesignChangeDomain> selectByDomainWhere(Page<DesignChangeDomain> page,
                                                        DesignChangeVo designChangeVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DesignChangeDomain>)designChangeDao.selectByDomainWhere(designChangeVo);
    }

    @Override
    public Page<DesignChangeDomain> selectMyCheckByDomainWhere(Page<DesignChangeDomain> page,
                                                               DesignChangeVo designChangeVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DesignChangeDomain>)designChangeDao.selectMyCheckByDomainWhere(designChangeVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDesignChange(DesignChange designChange,
                                 List<DesignChangeDetail> designChangeDetails,
                                 List<DesignChangeAtt> designChangeAtts) {
        designChangeDao.insertById(designChange);
        
        if(CollectionUtils.isNotEmpty(designChangeDetails)){
            designChangeDetailDao.insertByBatch(designChangeDetails);
        }
        
        if(CollectionUtils.isNotEmpty(designChangeAtts)){
            designChangeAttDao.insertByBatch(designChangeAtts);
        }
        
        // 20万
        BigDecimal cmp = new BigDecimal("200000");
        String srcBizTable = "IC_DESIGN_CHANGE";
        String branchCode_lt = "change_amount<=20万";
        String branchCode_gt = "change_amount>20万";
        String templateId = "";
        // <=20万
        if (designChange.getChangeAmount().compareTo(cmp) <= 0) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_lt);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_gt);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, designChange.getId(),
            designChange.getProcessId(), null);
    }

    @Override
    public DesignChangeVo queryById(String id) {
        return designChangeDao.queryById(id);
    }

}